<!DOCTYPE html>
<html>
<head>
<title>medicalRecordIframe.html</title>

<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">

<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

</head>
<style>
select {
	height: 38px;
	line-height: 1.3;
	line-height: 38px\9;
	border-width: 1px;
	border-style: solid;
	background-color: #fff;
	border-radius: 2px;
	width: 100%;
}

h1 {
	color: #c2c2c2;
	line-height: 25px;
	font-size: 16px;
	text-align: center;
	margin-top: 5px;
}

.row {
	margin-top: 10px;
}

.iCss {
	color: red;
	padding-right: 5px;
}

.hidden {
	display: none;
}
</style>
<link rel="stylesheet" href="/Hospital/lib/layui-v2.1.6/layui/css/layui.css">
<body>
	<div id="div_medicalRecord" class="layui-form layui-container" style="">
		<div class="layui-row row">
			<div class="layui-col-xs4 layui-col-xs-offset1 layui-col-md2 layui-col-md-offset2">
				<span class="layui-form-label"><i class="iCss">*</i>姓名:</span>
			</div>
			<div class="layui-col-xs5 layui-col-md2 ">
				<input type="text" lay-verify="required" name="name" id="name" class="layui-input" placeholder="请输入姓名">
			</div>
		</div>

		<div class="layui-row row">
			<div class="layui-col-xs4 layui-col-xs-offset1 layui-col-md2 layui-col-md-offset2">
				<span class="layui-form-label"><i class="iCss">*</i>性别: </span>
			</div>
			<div class="layui-col-xs5 layui-col-md2">
				<input type="radio" value="0" title="男" name="sex" checked=""> <input type="radio" value="1" title="女" name="sex">
			</div>
		</div>

		<div class="layui-row row">
			<div class="layui-col-xs4 layui-col-xs-offset1 layui-col-md2 layui-col-md-offset2">
				<span class="layui-form-label"><i class="iCss">*</i>身份证: </span>
			</div>
			<div class="layui-col-xs5 layui-col-md2">
				<input type="text" lay-verify="identity|required" name="identity" class="layui-input" placeholder="请输入身份证">
			</div>
		</div>

		<div class="layui-row row">
			<div class="layui-col-xs4 layui-col-xs-offset1 layui-col-md2 layui-col-md-offset2">
				<span class="layui-form-label"><i class="iCss">*</i>联系电话: </span>
			</div>
			<div class="layui-col-xs5 layui-col-md2">
				<input type="text" lay-verify="phone" name="phone" id="phone" class="layui-input" placeholder="请输入联系电话">
			</div>
		</div>

		<div class="layui-row row">
			<div class="layui-col-xs4 layui-col-xs-offset1 layui-col-md2 layui-col-md-offset2">
				<span class="layui-form-label"><i class="iCss">*</i>出生日期: </span>
			</div>
			<div class="layui-col-xs5 layui-col-md2">
				<input type="text" lay-verify="date" name="birthdate" id="birthdate" class="layui-input" placeholder="请选择出生日期">
			</div>
		</div>

		<div class="layui-row row">
			<div class="layui-col-xs4 layui-col-xs-offset1 layui-col-md2 layui-col-md-offset2">
				<span class="layui-form-label">民族: </span>
			</div>
			<div class="layui-col-xs5 layui-col-md2">
				<input type="text" name="nation" id="nation" class="layui-input" placeholder="请输入所属民族">
			</div>
		</div>

		<div class="layui-row row">
			<div class="layui-col-xs4 layui-col-xs-offset1 layui-col-md2 layui-col-md-offset2">
				<span class="layui-form-label">婚况: </span>
			</div>
			<div class="layui-col-xs5 layui-col-md2">
				<select name="marriage">
					<option value="0">未婚</option>
					<option value="1">已婚</option>
				</select>
			</div>
		</div>

		<div class="layui-row row">
			<div class="layui-col-xs4 layui-col-xs-offset1 layui-col-md2 layui-col-md-offset2">
				<span class="layui-form-label">职业: </span>
			</div>
			<div class="layui-col-xs5 layui-col-md2">
				<div class="layui-input-inline">
					<input type="text" name="occupation" id="occupation" class="layui-input" placeholder="请输入从事职业">
				</div>
			</div>
		</div>

		<div id="div_registerTime" class="layui-row row">
			<div class="layui-col-xs4 layui-col-xs-offset1 layui-col-md2 layui-col-md-offset2">
				<span class="layui-form-label">注册时间: </span>
			</div>
			<div class="layui-col-xs5 layui-col-md2">
				<input type="text" name="registerTime" id="registerTime" class="layui-input" disabled>
			</div>
		</div>

		<div class="layui-row row">
			<div class="layui-col-xs4 layui-col-xs-offset1 layui-col-md2 layui-col-md-offset2">
				<span class="layui-form-label">类型: </span>
			</div>
			<div class="layui-col-xs5 layui-col-md2">
				<select name="status">
					<option value="0">普通客户</option>
					<option value="1">普通会员客户</option>
					<option value="2">超级会员客户</option>
					<option value="-1">黑名单客户</option>
				</select>
			</div>
		</div>
		<input type="text" name="clientId" class="hidden"> <input type="button" id="submit" lay-submit lay-filter="addSubmit" class="hidden">
		<input type="button" id="updateSubmit" lay-submit lay-filter="updateSubmit" class="hidden">
	</div>
</body>
<script type="text/javascript" src="/Hospital/lib/jquery.min.js"></script>
<script type="text/javascript" src="/Hospital/lib/layui-v2.1.6/layui/layui.js"></script>
<script>
	var isSuccess = 0;
	var oldIdentity; //oldIdentity和identityNum用于判断编辑时身份证是否有改动，有改动时为0，进行唯一性核查
	var identityNum = 0; //没有改动时为1，不进行唯一性核查

	layui.use([ 'form', 'laydate' ],
			function() {
				var form = layui.form;
				var laydate = layui.laydate;
				var $ = layui.jquery;
				formInit(); //表单数据初始化
				listen(); //表单监听
				form.render();
				laydate.render({ //时间初始化
					elem : '#birthdate'
				});

				function formInit() {
					var ClientData = JSON.parse(localStorage.getItem("ClientData"));
					var editClientData = JSON.parse(localStorage.getItem("editClientData"));
					if (ClientData != null) { //当点击详情打开此页面时
						localStorage.removeItem("ClientData");
						//设置form表单为不可编辑
						$('input[name="name"]').attr("disabled", "disabled");
						$('input[name="identity"]').attr("disabled", "disabled");
						$('input[name="phone"]').attr("disabled", "disabled");
						$('input[name="birthdate"]').attr("disabled", "disabled");
						$('input[name="nation"]').attr("disabled", "disabled");
						$('select[name="marriage"]').attr("disabled", "disabled");
						$('input[name="occupation"]').attr("disabled", "disabled");
						$('input[name="registerTime"]').attr("disabled", "disabled");
						$('select[name="status"]').attr("disabled", "disabled");
						$('input[name="sex"]').attr("disabled", "disabled");
						$('#registerTimeTR').removeClass("hidden");
						//获取client详情信息
						$('input[name="name"]').val(ClientData.name);
						$('input[name="identity"]').val(ClientData.identity);
						$('input[name="sex"][value="' + ClientData.sexId + '"]').prop("checked",
								"checked");
						$('input[name="phone"]').val(ClientData.phone);
						$('input[name="birthdate"]').val(ClientData.birthdate);
						$('input[name="nation"]').val(ClientData.nation);
						$('select[name="marriage"]').val(ClientData.marriageId);
						$('input[name="occupation"]').val(ClientData.occupation);
						$('input[name="registerTime"]').val(ClientData.registerTime);
						$('select[name="status"]').val(ClientData.statusId);
					} else if (editClientData != null) { //当点击编辑打开此页面时
						localStorage.removeItem("editClientData");
						//获取client详情信息
						$('input[name="clientId"]').val(editClientData.clientId);
						$('input[name="name"]').val(editClientData.name);
						$('input[name="identity"]').val(editClientData.identity);
						$('input[name="sex"][value="' + editClientData.sexId + '"]').prop(
								"checked", "checked");
						$('input[name="phone"]').val(editClientData.phone);
						$('input[name="birthdate"]').val(editClientData.birthdate);
						$('input[name="nation"]').val(editClientData.nation);
						$('select[name="marriage"]').val(editClientData.marriageId);
						$('input[name="occupation"]').val(editClientData.occupation);
						$('input[name="registerTime"]').val(editClientData.registerTime);
						$('select[name="status"]').val(editClientData.statusId);

						oldIdentity = editClientData.identity; //获取传值的身份证的值
					}else {
						//点击添加打开此页面
						$("#div_registerTime").css("display","none");
					}
				}
				function listen() {
					form.on('submit(addSubmit)', function() { //提交监听
						var data = formData(identityNum);
						$.ajax({
							data : data,
							type : "POST",
							url : "/Hospital/userController/createClient",
							async : false,
							success : function(result) {
								if (result.code == 200) {
									layer.msg("添加成功！");
									isSuccess = 200;
									resetFormData();
								} else if (result.code == 411) {
									layer.msg("身份证已存在，不可重复");
								} else if (result.code == 501) {
									layer.msg("数据库操作失败");
								} else if (result.code == 403) {
									layer.msg("参数格式有误");
								} else if (result.code == 405) {
									layer.msg("请重新登录");
								} else if (result.code == 402) {
									layer.msg("权限不足");
								}
							}
						});
					});
					form.on('submit(updateSubmit)', function() { //更改按钮 监听事件
						var newIdentity = $('input[name="identity"]').val();
						var data;
						if (newIdentity == oldIdentity) {
							identityNum = 1;
						}
						var data = formData(identityNum);
						console.log("medicalRecordIframe.data");
						console.log(data);
						$.ajax({
							data : data,
							type : "POST",
							url : "/Hospital/userController/updateClient",
							async : false,
							success : function(result) {
								if (result.code == 200) {
									layer.msg("更改成功！");
									isSuccess = 200;
									//resetFormData();
								} else if (result.code == 411) {
									layer.msg("身份证已存在，不可重复");
								} else if (result.code == 501) {
									layer.msg("数据库操作失败");
								} else if (result.code == 403) {
									layer.msg("参数格式有误");
								} else if (result.code == 405) {
									layer.msg("请重新登录");
								} else if (result.code == 402) {
									layer.msg("权限不足");
								}
							}
						});
					});
				}
				function formData(identityNum) {
					var identityNum = identityNum;
					var clientId = $('input[name="clientId"]').val();
					var name = $('input[name="name"]').val();
					var identity = $('input[name="identity"]').val();
					var sex = $('input[name="sex"]:checked').val();
					var phone = $('input[name="phone"]').val();
					var birthdate = $('input[name="birthdate"]').val();
					var nation = $('input[name="nation"]').val();
					var marriage = $('select[name="marriage"]').find("option:selected").val();
					var occupation = $('input[name="occupation"]').val();
					var status = $('select[name="status"]').find("option:selected").val();
					var data = {
						"clientId" : clientId,
						"name" : name,
						"identity" : identity,
						"sex" : sex,
						"phone" : phone,
						"birthdate" : birthdate,
						"nation" : nation,
						"marriage" : marriage,
						"occupation" : occupation,
						"status" : status,
						"identityNum" : identityNum
					};
					return data;
				}
			});
	
	function resetFormData() {
		var name = $('input[name="name"]').val("");
		var identity = $('input[name="identity"]').val("");
		var sex = $('input[name="sex"]:first').attr("checked", true);
		var phone = $('input[name="phone"]').val("");
		var birthdate = $('input[name="birthdate"]').val("");
		var nation = $('input[name="nation"]').val("");
		var marriage = $('select[name="marriage"]').val(0);
		var occupation = $('input[name="occupation"]').val("");
		var status = $('select[name="status"]').val(0);
	}
	function submit() {
		$('#submit').click();
		var is = isSuccess;
		isSuccess = 0;
		return is;
	}

	function updateSubmit() {
		$('#updateSubmit').click();
		var is = isSuccess;
		isSuccess = 0;
		return is;
	}
</script>
</html>
